home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / upc12bs1.zip / UUCICO / nbstime.c < prev    next >
C/C++ Source or Header  |  1993-09-26  |  13KB  |  390 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    n b s t i m e . c                                               */
  3. /*                                                                    */
  4. /*    Set local system clock from National Bureau of Standards        */
  5. /*    Standard Time service                                           */
  6. /*--------------------------------------------------------------------*/
  7.  
  8. /*--------------------------------------------------------------------*/
  9. /*    Copyright (c) 1990-1993 by Kendra Electronic Wonderworks.       */
  10. /*                                                                    */
  11. /*    All rights reserved except those explicitly granted by the      */
  12. /*    UUPC/extended license agreement.                                */
  13. /*--------------------------------------------------------------------*/
  14.  
  15. /*--------------------------------------------------------------------*/
  16. /*                          RCS Information                           */
  17. /*--------------------------------------------------------------------*/
  18.  
  19. /*
  20.  *    $Id: nbstime.c 1.10 1993/09/26 03:32:27 dmwatt Exp $
  21.  *
  22.  *    Revision history:
  23.  *    $Log: nbstime.c $
  24.  * Revision 1.10  1993/09/26  03:32:27  dmwatt
  25.  * Use Standard Windows NT error message module
  26.  *
  27.  * Revision 1.9  1993/09/20  04:46:34  ahd
  28.  * OS/2 2.x support (BC++ 1.0 support)
  29.  * TCP/IP support from Dave Watt
  30.  * 't' protocol support
  31.  *
  32.  * Revision 1.8  1993/07/22  23:22:27  ahd
  33.  * First pass at changes for Robert Denny's Windows 3.1 support
  34.  *
  35.  * Revision 1.7  1993/06/13  14:02:32  dmwatt
  36.  * Additional Windows/NT fixes
  37.  *
  38.  * Revision 1.6  1993/05/30  00:04:53  ahd
  39.  * Multiple communications drivers support
  40.  *
  41.  * Revision 1.5  1993/04/15  04:15:43  ahd
  42.  * Twiddle OS/2 support and reduce number of #ifdef segments
  43.  *
  44.  * Revision 1.4  1993/04/13  02:27:31  dmwatt
  45.  * Windows/NT updates
  46.  *
  47.  * Revision 1.3  1993/04/11  00:34:11  ahd
  48.  * Global edits for year, TEXT, etc.
  49.  *
  50.  */
  51.  
  52. /*--------------------------------------------------------------------*/
  53. /*                        System include files                        */
  54. /*--------------------------------------------------------------------*/
  55.  
  56. #include <stdio.h>
  57. #include <stdlib.h>
  58. #include <time.h>
  59. #include <string.h>
  60.  
  61.  
  62. #ifdef WIN32
  63.  
  64. #include <windows.h>
  65. #define NONDOS
  66.  
  67. #elif defined(FAMILYAPI) || defined(__OS2__)
  68.  
  69. #define NONDOS
  70. #define INCL_BASE
  71.  
  72. #include <os2.h>
  73.  
  74. #elif !defined(__TURBOC__)
  75.  
  76. #include <dos.h>
  77.  
  78. #endif /* WIN32 */
  79.  
  80.  
  81. /*--------------------------------------------------------------------*/
  82. /*                    UUPC/extended include files                     */
  83. /*--------------------------------------------------------------------*/
  84.  
  85. #include "lib.h"
  86. #include "arpadate.h"
  87. #include "dcp.h"
  88. #include "dcpsys.h"
  89. #include "hostable.h"
  90. #include "nbstime.h"
  91. #include "script.h"
  92. #include "security.h"
  93. #include "commlib.h"
  94.  
  95. #if !defined(__TURBOC__) || defined(__OS2__)
  96.    currentfile();
  97. #endif
  98.  
  99. #if defined(WIN32)
  100. #include "pnterr.h"
  101. #endif
  102.  
  103. /*--------------------------------------------------------------------*/
  104. /*    n b s t i m e                                                   */
  105. /*                                                                    */
  106. /*    Set system clock from using time from NBS of the format:        */
  107. /*                                                                    */
  108. /*                  MJD  YR MO DA  H  M  S ST S UT1 msADV         OTM */
  109. /*  nbs format-->  47511 88-12-16 06:03:44 00 0 -.1 045.0 UTC(NIST) * */
  110. /*  @ 1200 baud    47511 88-12-16 06:03:45 00 0 -.1 045.0 UTC(NIST) * */
  111. /*--------------------------------------------------------------------*/
  112.  
  113. boolean nbstime( void )
  114. {
  115.    char buf[BUFSIZ];
  116.    time_t today;
  117.    struct tm  tx;
  118.    int cycles = 15;
  119.    int dst= 0;
  120.    time_t delta;
  121.    char sync = '?';
  122.  
  123. #ifdef WIN32
  124.  
  125.    SYSTEMTIME DateTime;
  126.    TOKEN_PRIVILEGES tkp;
  127.    HANDLE hToken;
  128.    USHORT rc;
  129.    DWORD dwError;
  130.  
  131. #elif defined(FAMILYAPI) || defined(__OS2__)
  132.  
  133.    DATETIME DateTime;
  134.    struct tm *tp;
  135.    USHORT rc;
  136.  
  137. #elif !defined(NONDOS) && !defined(__TURBOC__)
  138.  
  139.    unsigned short rc;
  140.    struct dosdate_t ddate;
  141.    struct dostime_t dtime;
  142.    struct tm *tp;
  143.  
  144. #endif
  145.  
  146.    memset( &tx , '\0', sizeof tx);        /* Clear pointers          */
  147.    if (!expectstr("MJD", 5, NULL )) /* Margaret Jane Derbyshire? :-) */
  148.    {
  149.       printmsg(0,"nbstime: Did not find MJD literal in data from remote");
  150.       return FALSE;
  151.    }
  152.  
  153.    rmsg(buf, 2, 2, sizeof buf);
  154.                   /* Read one line to get us setup for input   */
  155.  
  156. /*--------------------------------------------------------------------*/
  157. /*                  Begin main loop to get the time                   */
  158. /*--------------------------------------------------------------------*/
  159.  
  160.    while ((rmsg(buf, 2, 2, sizeof buf) != TIMEOUT) && cycles--)
  161.    {
  162.       sync = buf[ strlen( buf ) - 1 ];
  163.  
  164.       if (sync == '#')
  165.          break;
  166.       else if (sync != '*')
  167.          *buf = '\0';
  168.  
  169.    } /* while */
  170.  
  171.    if ( (cycles && (sync == '*')) || (*buf == '\0'))
  172.    {
  173.       printmsg(0,"nbstime: Did not get good buffer: \"%s\"", buf );
  174.                   return FALSE;
  175.    }
  176.  
  177. /*--------------------------------------------------------------------*/
  178. /*                   Determine the time we received                   */
  179. /*--------------------------------------------------------------------*/
  180.  
  181.    sscanf(buf,"%*s %d-%d-%d %d:%d:%d %d ",
  182.          &tx.tm_year, &tx.tm_mon, &tx.tm_mday ,
  183.          &tx.tm_hour, &tx.tm_min, &tx.tm_sec, &dst);
  184.    tx.tm_mon--;               /* Tm record counts months from zero   */
  185.  
  186. /*--------------------------------------------------------------------*/
  187. /*     mktime()'s "renormalizing" the tm struct is screwing up NT    */
  188. /*--------------------------------------------------------------------*/
  189. #if !defined(WIN32)
  190.    today = mktime(&tx);       /* Current UTC (GMT) time in seconds   */
  191.  
  192.    if ( debuglevel > 2 )
  193.    {
  194.       printmsg(3,"%2d/%2d/%2d %2d:%2d:%2d %2d %c translates to %ld or %s",
  195.          tx.tm_year, tx.tm_mon + 1 , tx.tm_mday ,
  196.          tx.tm_hour, tx.tm_min, tx.tm_sec, dst, sync ,
  197.          today, ctime( &today ));
  198.    }
  199.  
  200. /*--------------------------------------------------------------------*/
  201. /*    Perform a sanity check; the time must be 20 years past 1970     */
  202. /*--------------------------------------------------------------------*/
  203.  
  204.    if ( today < 630720000L )
  205.    {
  206.       printmsg(0,"nbstime: Time warp error (%s), clock not set",
  207.             ctime( &today ));
  208.       return FALSE;
  209.    }
  210. #endif
  211.  
  212. /*--------------------------------------------------------------------*/
  213. /*                        Set the system clock                        */
  214. /*--------------------------------------------------------------------*/
  215.  
  216. #if defined(FAMILYAPI) || defined(__OS2__)
  217.  
  218.    rc = DosGetDateTime( &DateTime );
  219.    if ( rc != 0 )
  220.    {
  221.       printmsg(0,"Return code from DosGetDateTime %d", rc);
  222.       panic();
  223.    }
  224.  
  225.    printmsg(3,"Date time: %2d/%2d/%2d %2d:%2d:%2d tz %d, weekday %d",
  226.       (int) DateTime.year, (int) DateTime.month, (int) DateTime.day ,
  227.       (int) DateTime.hours, (int) DateTime.minutes,(int) DateTime.seconds ,
  228.       (int) DateTime.timezone, (int) DateTime.weekday );
  229.  
  230.    today -= timezone;
  231.    tp = localtime(&today);    /* Get local time as a record          */
  232.  
  233.    DateTime.year    = (USHORT) tp->tm_year + 1900;
  234.    DateTime.month   = (UCHAR) (tp->tm_mon + 1);
  235.    DateTime.day     = (UCHAR) tp->tm_mday;
  236.    DateTime.hours   = (UCHAR) tp->tm_hour;
  237.    DateTime.minutes = (UCHAR) tp->tm_min;
  238.    DateTime.seconds = (UCHAR) tp->tm_sec;
  239.  
  240.    printmsg(3,"Date time: %2d/%2d/%2d %2d:%2d:%2d tz %d, weekday %d",
  241.       (int) DateTime.year, (int) DateTime.month, (int) DateTime.day ,
  242.       (int) DateTime.hours, (int) DateTime.minutes,(int) DateTime.seconds ,
  243.       (int) DateTime.timezone, (int) DateTime.weekday );
  244.  
  245.    rc = DosSetDateTime( &DateTime );
  246.    if ( rc != 0 )
  247.    {
  248.       printmsg(0,"Return code from DosGetDateTime %d", rc);
  249.       panic();
  250.    }
  251.  
  252. #elif defined( WIN32 )
  253.  
  254.    GetSystemTime( &DateTime );
  255.  
  256.    printmsg(3,"Date time: %2d/%2d/%2d %2d:%2d:%2d, weekday %d",
  257.       (int) DateTime.wYear, (int) DateTime.wMonth, (int) DateTime.wDay ,
  258.       (int) DateTime.wHour, (int) DateTime.wMinute,(int) DateTime.wSecond ,
  259.       (int) DateTime.wDayOfWeek );
  260.  
  261.    DateTime.wYear    = (WORD) tx.tm_year + 1900;
  262.    DateTime.wMonth   = (WORD) tx.tm_mon + 1;
  263.    DateTime.wDay     = (WORD) tx.tm_mday;
  264.    DateTime.wHour    = (WORD) tx.tm_hour;
  265.    DateTime.wMinute  = (WORD) tx.tm_min;
  266.    DateTime.wSecond  = (WORD) tx.tm_sec;
  267.  
  268.    printmsg(3,"Date time: %2d/%2d/%2d %2d:%2d:%2d, weekday %d",
  269.       (int) DateTime.wYear, (int) DateTime.wMonth, (int) DateTime.wDay ,
  270.       (int) DateTime.wHour, (int) DateTime.wMinute, (int) DateTime.wSecond ,
  271.       (int) DateTime.wDayOfWeek );
  272.  
  273.    if (!OpenProcessToken(GetCurrentProcess(),
  274.       TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
  275.       &hToken))
  276.    {
  277.       dwError = GetLastError();
  278.       printmsg(0, "nbstime: OpenProcessToken failed");
  279.       printNTerror("OpenProcessToken", dwError);
  280.       return FALSE;
  281.    }
  282.  
  283.    LookupPrivilegeValue(NULL, "SeSystemtimePrivilege",
  284.       &tkp.Privileges[0].Luid);
  285.    tkp.PrivilegeCount = 1;
  286.    tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  287.  
  288.    if (!AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
  289.       (PTOKEN_PRIVILEGES)NULL, 0))
  290.    {
  291.       dwError = GetLastError();
  292.       printmsg(0, "nbstime: first AdjustTokenPrivilege failed");
  293.       printNTerror("AdjustTokenPrivileges", dwError);
  294.       return FALSE;
  295.    }
  296.  
  297.    rc = SetSystemTime( &DateTime );
  298.    if ( !rc )
  299.    {
  300.       dwError = GetLastError();
  301.       printmsg(0, "nbstime: SetSystemTime failed");
  302.       printNTerror("SetSystemTime", dwError);
  303.    }
  304.  
  305.    tkp.Privileges[0].Attributes = 0;
  306.  
  307.    if (!AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
  308.       (PTOKEN_PRIVILEGES)NULL, 0))
  309.    {
  310.       dwError = GetLastError();
  311.       printmsg(0, "nbstime: AdjustTokenPrivileges disable failed");
  312.       printNTerror("AdjustTokenPrivileges", dwError);
  313.       return FALSE;
  314.    }
  315.  
  316. #elif defined( __TURBOC__ )
  317.  
  318. /*--------------------------------------------------------------------*/
  319. /*     Borland C++ doesn't set the time properly; do a conversion     */
  320. /*--------------------------------------------------------------------*/
  321.  
  322.    today -= timezone;
  323.  
  324. /*--------------------------------------------------------------------*/
  325. /*    If this timezone uses daylight savings and we are in the        */
  326. /*    period to spring forward, do so.                                */
  327. /*--------------------------------------------------------------------*/
  328.  
  329.    if (daylight && ( dst > 1 ) && ( dst < 52 ))
  330.       today += 3600;          /* This is valid for the USA only      */
  331.    stime( &today );
  332.  
  333. #else /* __TURBOC__ */
  334.  
  335.  
  336.    tp = localtime(&today);    /* Get local time as a record          */
  337.  
  338.    ddate.day     = (unsigned char) tp->tm_mday;
  339.    ddate.month   = (unsigned char) (tp->tm_mon + 1);
  340.    ddate.year    = (unsigned int)  (tp->tm_year + 1900);
  341.    ddate.dayofweek = (unsigned char) tp->tm_mday;       /* 0-6, 0=Sunday */
  342.  
  343.    dtime.hour    = (unsigned char) tp->tm_hour;
  344.    dtime.minute  = (unsigned char) tp->tm_min;
  345.    dtime.second  = (unsigned char) tp->tm_sec;
  346.    dtime.hsecond = (unsigned char) 0;
  347.  
  348.    printmsg(3,"Date time: %2d/%2d/%2d %2d:%2d:%2d tz %d, weekday %d",
  349.       (int) ddate.year, (int) ddate.month, (int) ddate.day ,
  350.       (int) dtime.hour, (int) dtime.minute,(int) dtime.second ,
  351.       (int) timezone, (int) ddate.dayofweek );
  352.  
  353.    if ( (rc = _dos_settime( &dtime )) != 0 )
  354.    {
  355.       printmsg(0,"Return code from _dos_settime %d", rc);
  356.       panic();
  357.    }
  358.  
  359.    if ( (rc = _dos_setdate( &ddate )) != 0 )
  360.    {
  361.       printmsg(0,"Return code from _dos_setdate %d", rc);
  362.       panic();
  363.    }
  364.  
  365. #endif
  366.  
  367. /*--------------------------------------------------------------------*/
  368. /*             Print debugging information, if requested              */
  369. /*--------------------------------------------------------------------*/
  370.  
  371. #if !defined(WIN32)
  372.    delta = today - time( NULL );
  373.    printmsg(2,"nbstime: \"%s\"", buf);
  374.    printmsg(2,"nbstime: Time delta is %ld seconds, zone offset %ld, "
  375.               "daylight savings %d",
  376.                   delta, timezone, dst );
  377. #endif
  378.  
  379.    if ( sync == '*' )
  380.       printmsg(2,"Warning: Was unable to synchonize with NBS master");
  381.  
  382. /*--------------------------------------------------------------------*/
  383. /*                Announce new time, return to caller                 */
  384. /*--------------------------------------------------------------------*/
  385.  
  386.    printmsg(0,"nbstime: New system time is %s", arpadate());
  387.    return TRUE;
  388.  
  389. } /* nbstime */
  390.